__author__ = 'Soul'

from core.JsonDataFetcher import *
from dboperater.DbOperatorFactory import *
import string
import time

class YingyongBaoFetcher(JsonDataFetcher):
    def init(self, url, intent=False, recurse=False):
        factory = DbOperatorFactory()
        self.__db__ = factory.GetDbOperator('appdb')
        return JsonDataFetcher.init(self, url, True)

    def preProcess(self):
        self.__data_src__ = self.__data_src__.decode('utf-8')

    def analysis(self):
        if self.__json_data__:
            global seen_urls, urls_to_visit
            app_items = self.__json_data__['obj']['items']
            for app_info in app_items:
                app_detail = app_info['appDetail']

                app_name = app_detail['appName']
                app_star = app_detail['averageRating']
                app_pbl_ent = app_detail['authorName']
                app_desc = app_detail['description']

                app_size = app_detail["fileSize"]
                app_pbl_date = app_detail['apkPublishTime']
                app_down_count = app_detail['appDownCount']

                app_record = {
                    'app_name': app_name,
                    'app_desc': app_desc,
                    'app_star': app_star,
                    'app_src': '',
                    'app_size': app_size,
                    'app_pbl_ent': app_pbl_ent,
                    'app_pbl_date': app_pbl_date,
                    'app_down_count': app_down_count,
                    'app_fetch_dt': (int)(time.time())
                }

                self.__db__.insert_record(app_record)

            # calculate next url
            next_pns = self.__json_data__['obj']['pageNumberStack']
            # get the next page URL
            url_str = self.__url__
            items = url_str.split('&')
            current_pns = items[1]
            if not current_pns == 'NTA=':
                url_prefix = items[0]
                next_url = "%s&pns=%s&sid=0" %(url_prefix, next_pns)
                urls_to_visit.append({"url": next_url, "controller": "YingyongBaoFetcher"})